package com.wuliupai.delivery.wuliupaidelivery.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.mapapi.search.sug.SuggestionResult;
import com.wuliupai.delivery.wuliupaidelivery.R;

import java.util.List;

/**
 * Descriptions：百度地图在线建议查询返回结果的Adapter
 * <p>
 * Author：ChenME
 * Date：2017/3/16
 * Email：ibelieve1210@163.com
 */
public class BaiduMapPointSuggestionAdapter extends BaseAdapter {

    private Context mContext;
    private List<SuggestionResult.SuggestionInfo> dataList;

    public BaiduMapPointSuggestionAdapter(Context mContext, List<SuggestionResult.SuggestionInfo> dataList) {
        this.mContext = mContext;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return null == dataList ? 0 : dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vHolder;
        if (null == convertView) {
            vHolder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_place_search_lv, null);
            vHolder.tv_searchResult = (TextView) convertView.findViewById(R.id.tv_searchResult);
            vHolder.rl_result = (RelativeLayout) convertView.findViewById(R.id.rl_result);
            convertView.setTag(vHolder);
        } else {
            vHolder = (ViewHolder) convertView.getTag();
        }

        SuggestionResult.SuggestionInfo info = dataList.get(position);
        vHolder.rl_result.setBackgroundColor(mContext.getResources().getColor(R.color.color_listBackground));
        vHolder.tv_searchResult.setText(getSuggestionInfo(info));

        return convertView;
    }

    public String getSuggestionInfo(SuggestionResult.SuggestionInfo info){
        return info.key + " - " + info.city + "," + info.district;
    }

    class ViewHolder {
        private TextView tv_searchResult;
        private RelativeLayout rl_result;
    }
}